package de.tabuma.sso.customer;

import de.tabuma.sso.*;
import de.tabuma.sso.project.*;
import de.tabuma.sso.group.*;
import de.tabuma.sso.sso.*;
import de.tabuma.sso.user.*;
import de.tabuma.sso.useruserrelation.*;
import de.tabuma.customer.entity.*;
import de.tabuma.customer.logik.*;
import de.tabuma.shopping.logik.*;
import java.util.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class CustomerWebservice {
    public static int create(Customer customer, String ssoId) throws Exception {
        int groupId = CustomerWebserviceLib.userHasPrivileg(ssoId);

        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.CUSTOMER_CREATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        CustomerWebserviceLib.userHasPrivileg(ssoId);
        int customerId = CustomerFacade.create(customer, user.getName());
        List<UserUserRelation> uurList = UserUserRelationFacade.getList(user.getId().intValue());
        for(int i=0;i<uurList.size();i++) {
            CustomerUserRelation cur = new CustomerUserRelation();
            cur.setCostumerId(customerId);
            cur.setUser(uurList.get(i).getUser());
            CustomerUserRelationFacade.create(cur, ssoId);
        }
        return customerId;
    }

    public static int create1(Customer customer, String ssoId, String username) throws Exception {
        int groupId = CustomerWebserviceLib.userHasPrivileg(ssoId);

        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.CUSTOMER_CREATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        CustomerWebserviceLib.userHasPrivileg(ssoId);
        int customerId = CustomerFacade.create(customer, user.getName());
        CustomerUserRelation cur = new CustomerUserRelation();
        cur.setCostumerId(customerId);
        cur.setUser(username);
        CustomerUserRelationFacade.create(cur, username);
        return customerId;
    }

    public static void update(Customer customer, String ssoId) throws Exception {
        int groupId = CustomerWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.CUSTOMER_UPDATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Customer oldCustomer = CustomerFacade.get(customer.getId().intValue());
        CustomerWebserviceLib.userHasPrivilegForCustomer(customer, ssoId);
        CustomerWebserviceLib.userHasPrivilegForCustomer(oldCustomer, ssoId);
        CustomerFacade.update(customer, user.getName());
    }

    public static Customer get(int id, String ssoId) throws Exception {
        int groupId = CustomerWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.CUSTOMER_GET, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        Customer customer = CustomerFacade.get(id);
        CustomerWebserviceLib.userHasPrivilegForCustomer(customer, ssoId);

        return customer;
    }

    public static List<Customer> getList(String ssoId) throws Exception {
        int groupId = CustomerWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.CUSTOMER_GETLIST, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        List<UserUserRelation> uurList = UserUserRelationFacade.getList(sso.getUserId());
        List<Customer> cList = new ArrayList<Customer>();
        for(int i=0;i<uurList.size();i++) {
            cList.addAll(CustomerFacade.getCustomer(uurList.get(i).getUser()));
        }
        return cList;
    }

    public static void remove(int id, String ssoId) throws Exception {
        int groupId = CustomerWebserviceLib.userHasPrivileg(ssoId);
        Customer customer = CustomerFacade.get(id);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.CUSTOMER_REMOVE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        CustomerWebserviceLib.userHasPrivilegForCustomer(customer, ssoId);
        CustomerFacade.remove(customer, user.getName());

        List<Group> gList = GroupFacade.getList(id);
        for(int i=0;i<gList.size();i++) {
            Group group = gList.get(i);
            GroupFacade.remove(group.getId().intValue(), user.getName());
        }

        List<Project> pList = ProjectFacade.getList(id);
        for(int i=0;i<pList.size();i++) {
            Project project = pList.get(i);
            ProjectFacade.remove(project.getId().intValue(), user.getName());
        }

    }


}